/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package almacen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.logging.Level;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import preparedstatement.DebugLevel;
import preparedstatement.StatementFactory;
import resources.local_connection;
import resources.queries;

/**
 *
 * @author Homeroe
 */
public class precios_new extends javax.swing.JFrame {

    private final int codigo, sucursal;
    PreparedStatement ps;
    ResultSet rs;
    int nprecios;
    JTextField[][] textFields;
    JComboBox[] combos;
    JLabel[] sugerencias;
    JButton[] cancelares;
    NumberFormat formatter = NumberFormat.getCurrencyInstance();

    public precios_new(int code, int suc) {
        initComponents();
        codigo = code;
        sucursal = suc;

        cargarDatos();
        estimarGanancia();
        /* JLabel temp = new JLabel("Row 5"); 
         temp.setVisible(true);
         JLabel temp2 = new JLabel("Row 6"); 
         temp.setVisible(true);
         javax.swing.GroupLayout panelPreciosLayout = new javax.swing.GroupLayout(panelPrecios);
         panelPrecios.setLayout(panelPreciosLayout);
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(123, 123, 123)
         .addComponent(temp)
         .addGap(0, 173, Short.MAX_VALUE);
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(123, 123, 123)
         .addComponent(temp2)
         .addGap(0, 173, Short.MAX_VALUE);
         panelPreciosLayout.setHorizontalGroup(
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         );
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(123, 123, 123)
         .addComponent(temp)
         .addGap(0, 173, Short.MAX_VALUE);
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(123, 123, 123)
         .addComponent(temp2)
         .addGap(0, 173, Short.MAX_VALUE);
         panelPreciosLayout.setVerticalGroup(
         panelPreciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         );
         panelPrecios.revalidate();
         panelPrecios.repaint();
         pack();*/
    }

    private void cargarDatos() {
        try {
            ps = (PreparedStatement) local_connection.getconnect().prepareStatement("select preciocompra,nombre from productos where id_producto = ?");
            ps.setInt(1, codigo);
            rs = ps.executeQuery();
            if (rs.next()) {
                textPrecioCompra.setText(rs.getString(1));
                nombre.setText(rs.getString(2));
            }
            ps = (PreparedStatement) local_connection.getconnect().prepareStatement("select nombre from sucursales where id = ?");
            ps.setInt(1, sucursal);
            rs = ps.executeQuery();
            rs.next();
            this.setTitle("Sucursal " + rs.getString(1));
            panelPrecios.removeAll();
            // GridBagConstraints textFieldConstraints = new GridBagConstraints();
            int i, j;
            ps = (PreparedStatement) local_connection.getconnect().prepareStatement("select count(*) from precios where sucursal = ? and id_producto = ?");
            ps.setInt(1, sucursal);
            ps.setInt(2, codigo);
            rs = ps.executeQuery();
            if (rs.next()) {
                if (rs.getInt(1) != 0) {
                    nprecios = rs.getInt(1) + 1;
                } //System.out.println(rs.getInt(1));
                else {
                    nprecios = 2;
                }
            }
            //System.out.println("nprecios "+nprecios);
            float[] cantidad = new float[nprecios];
            float[] precios = new float[nprecios];
            String[] nombres = new String[nprecios];
            ps = (PreparedStatement) local_connection.getconnect().prepareStatement("select nombre, cantidad, precio from precios where sucursal = ? and id_producto = ? order by cantidad asc");
            ps.setInt(1, sucursal);
            ps.setInt(2, codigo);
            rs = ps.executeQuery();

            int n = 0;//esta variable checa si hay registros
            while (rs.next()) {
                nombres[n] = rs.getString("nombre");
                cantidad[n] = rs.getFloat("cantidad");
                precios[n] = rs.getFloat("precio");
                n++;
            }
            if (n == 0) {
                nombres[0] = "Unitario";
                cantidad[0] = 1.0f;
                precios[0] = 1.0f;
            }
            panelPrecios.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            textFields = new JTextField[nprecios][3];
            combos = new JComboBox[nprecios];
            sugerencias = new JLabel[nprecios];
            cancelares = new JButton[nprecios];
            c.weightx = .1;
           //c.ipadx = 100;
            //c.anchor = GridBagConstraints.PAGE_START;
            //c.fill =GridBagConstraints.PAGE_START;
            // c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(3, 3, 3, 3);
            for (i = 0; i < nprecios; i++) {
                combos[i] = new JComboBox();
                cancelares[i] = new JButton();
                sugerencias[i] = new JLabel();
                for (j = 0; j < 3; j++) {
                    textFields[i][j] = new JTextField();
                    if (j < 1) {
                        textFields[i][j].setPreferredSize(new Dimension(80, 20));
                    } else {
                        textFields[i][j].setPreferredSize(new Dimension(50, 20));
                    }
                    if (j > 0) {
                        textFields[i][j].setDocument(new LimitadorCaracteresFloat());
                    }
                    c.gridx = j;
                    c.gridy = i;

                    /*textFieldConstraints.gridx = i;
                     textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                     textFieldConstraints.weightx = 0.5;
                     textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                     textFieldConstraints.gridy = j;*/
                    //    textFields[i][j].setPreferredSize(new Dimension(100, 20));
                    panelPrecios.add(textFields[i][j], c);
                    if (i < (nprecios - 1)) {
                        if (j == 0) {
                            textFields[i][j].setText(nombres[i]);
                        }
                        if (j == 1) {
                            textFields[i][j].setText(String.valueOf(cantidad[i]));
                        }
                        if (j == 2) {
                            textFields[i][j].setText(String.valueOf(precios[i]));
                        }
                    }
                }
                if (i != 0) {
                    c.gridx = 4;
                    float origen = Float.parseFloat(textFields[0][2].getText());

                    combos[i].addItem("5%");
                    combos[i].addItem("10%");
                    combos[i].addItem("15%");
                    combos[i].addItem("20%");
                    combos[i].addItem("25%");
                    combos[i].addItem("30%");
                    combos[i].addItemListener(new ItemChangeListener());
                    combos[i].setPreferredSize(new Dimension(50, 20));
                    // c.fill = GridBagConstraints.NONE;
                    panelPrecios.add(combos[i], c);
                    //combos[i].setPrototypeDisplayValue("XXXX");
                    c.gridx = 5;
                    if (!textFields[0][2].getText().equals("")) {
                        sugerencias[i].setText(formatter.format(origen - origen * porcentajeMayoreos(combos[i].getSelectedIndex()) / 100));
                    } else {
                        sugerencias[i].setText("");
                    }

                    panelPrecios.add(sugerencias[i], c);

                    c.gridx = 6;
                    cancelares[i].setText("X");
                    cancelares[i].setForeground(Color.red);
                    cancelares[i].addActionListener(new cancelare());
                    //   cancelares[i].setPreferredSize(new Dimension(50, 20));
                    cancelares[i].setMaximumSize(new Dimension(20, 20));
                    panelPrecios.add(cancelares[i], c);
                }

            }

            panelPrecios.updateUI();
            //  panelPrecios.revalidate();
            this.pack();
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
    }

    class ItemChangeListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent event) {

            if (event.getStateChange() == ItemEvent.SELECTED) {
                float origen = Float.parseFloat(textFields[0][2].getText());
                for (int i = 0; i < nprecios; i++) {
                    if (combos[i] == event.getSource()) {
                        if (!textFields[0][2].getText().equals("")) {
                            sugerencias[i].setText(formatter.format(origen - origen * porcentajeMayoreos(combos[i].getSelectedIndex()) / 100));
                        } else {
                            sugerencias[i].setText("");
                        }
                    }
                }
            }
        }

    }

    class cancelare implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            for (int i = 0; i < nprecios; i++) {
                if (cancelares[i] == ae.getSource()){
              //      System.out.println("aqui");
                  //  panelPrecios.remove(i);
                    panelPrecios.remove(cancelares[i]);
                    panelPrecios.remove(sugerencias[i]);
                    panelPrecios.remove(combos[i]);
                    textFields[i][0].setText("");
                    textFields[i][1].setText("");
                    textFields[i][2].setText("");
                    panelPrecios.remove(textFields[i][0]);
                    panelPrecios.remove(textFields[i][1]);
                    panelPrecios.remove(textFields[i][2]);
                    panelPrecios.updateUI();
              panelPrecios.revalidate();
              pack();
                }
            }
        }
    }

    float porcentajeMayoreos(int num) {
        switch (num) {
            case 1:
                return 10;
            case 2:
                return 15;
            case 3:
                return 20;
            case 4:
                return 25;
            case 5:
                return 30;
            default:
                return 5;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelPrecios = new javax.swing.JLabel();
        labelPrecioCompra = new javax.swing.JLabel();
        textPrecioCompra = new javax.swing.JTextField();
        comboPrecioCompra = new javax.swing.JComboBox();
        panelPrecios = new javax.swing.JPanel();
        buttonAceptar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        labelPreciosPublico = new javax.swing.JLabel();
        estimado = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Precios");

        labelPrecios.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelPrecios.setText("Precios:");

        labelPrecioCompra.setText("Precio de compra: $");

        textPrecioCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textPrecioCompraActionPerformed(evt);
            }
        });
        textPrecioCompra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textPrecioCompraKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textPrecioCompraKeyReleased(evt);
            }
        });

        comboPrecioCompra.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "20%", "30%", "50%", "100%", "150%", "200%", "250%", "300%", "350%", "400%", "450%" }));
        comboPrecioCompra.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                comboPrecioCompraPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        panelPrecios.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelPrecios.setLayout(new java.awt.GridBagLayout());

        buttonAceptar.setText("Guardar");
        buttonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAceptarActionPerformed(evt);
            }
        });

        buttonCancelar.setText("Cancelar");
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        labelPreciosPublico.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelPreciosPublico.setText("Precios al publico:");

        jLabel1.setText("Nombre:");

        jLabel2.setText("Cantidad:");

        jLabel3.setText("Precio:");

        jLabel4.setText("Sugerencia:");

        jLabel5.setText("Descuento:");

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(buttonAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelPrecioCompra)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addGap(29, 29, 29)
                                .addComponent(jLabel3)
                                .addGap(13, 13, 13)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(comboPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(estimado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(panelPrecios, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelPrecios)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(labelPreciosPublico))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelPrecios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPrecioCompra)
                    .addComponent(textPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboPrecioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(estimado, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPreciosPublico)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPrecios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancelar)
                    .addComponent(jButton1)
                    .addComponent(buttonAceptar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textPrecioCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textPrecioCompraActionPerformed

        // TODO add your handling code here:
    }//GEN-LAST:event_textPrecioCompraActionPerformed

    private void textPrecioCompraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textPrecioCompraKeyPressed

        // TODO add your handling code here:
    }//GEN-LAST:event_textPrecioCompraKeyPressed

    private void textPrecioCompraKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textPrecioCompraKeyReleased

    }//GEN-LAST:event_textPrecioCompraKeyReleased

    private void comboPrecioCompraPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_comboPrecioCompraPopupMenuWillBecomeInvisible
        estimarGanancia();
    }//GEN-LAST:event_comboPrecioCompraPopupMenuWillBecomeInvisible
    private void estimarGanancia() {
        if (!textPrecioCompra.getText().equals("")) {
            float cantidad, total;
            String porcentaje = (String) comboPrecioCompra.getSelectedItem();
            cantidad = Float.parseFloat(textPrecioCompra.getText());
            total = cantidad;
            total *= Integer.valueOf(porcentaje.substring(0, porcentaje.length() - 1));
            total /= 100;
            total += cantidad;
            estimado.setText(formatter.format(total));
        }
    }
    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_buttonCancelarActionPerformed

    private void buttonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAceptarActionPerformed
        guardarDatos();
        this.dispose();
    }//GEN-LAST:event_buttonAceptarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        guardarDatos();
        cargarDatos();
        estimarGanancia();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
    private void guardarDatos() {
        try {
            ps = StatementFactory.getStatement(local_connection.getconnect(), "DELETE FROM precios WHERE id_producto = ? AND sucursal = ?", DebugLevel.ON);
            ps.setInt(1, codigo);
            ps.setInt(2, sucursal);
            queries.doSyncQuery(local_connection.getconnect(), ps, ps.toString());
            ps = StatementFactory.getStatement(local_connection.getconnect(), "INSERT INTO precios(id_producto,sucursal,nombre,cantidad,precio) VALUES(?,?,?,?,?)", DebugLevel.ON);
            ps.setInt(1, codigo);
            ps.setInt(2, sucursal);
            for (int i = 0; i < nprecios; i++) {
                if ((!textFields[i][0].getText().equals("")) || (!textFields[i][0].getText().equals("")) || (!textFields[i][0].getText().equals(""))) {
                    ps.setString(3, textFields[i][0].getText());
                    //System.out.println("1"+textFields[0][i].getText());
                    ps.setString(4, textFields[i][1].getText());
                    //System.out.println("2"+textFields[1][i].getText());
                    ps.setString(5, textFields[i][2].getText());
                    // System.out.println("3"+textFields[2][i].getText());
                    queries.doSyncQuery(local_connection.getconnect(), ps, ps.toString());
                }
            }
            ps = StatementFactory.getStatement(local_connection.getconnect(), "UPDATE productos SET preciocompra = " + textPrecioCompra.getText() + " WHERE id_producto = " + codigo, DebugLevel.ON);
            queries.doSyncQuery(local_connection.getconnect(), ps, ps.toString());
        } catch (SQLException ex) {
Almacen.logger.log(Level.SEVERE, null, ex);        }
    }

    public class LimitadorCaracteresFloat extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         * @param arg0
         * @param arg1
         * @param arg2
         * @throws javax.swing.text.BadLocationException
         */
        @Override
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!(Character.isDigit(arg1.charAt(i)) || arg1.charAt(i) == '.')) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAceptar;
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JComboBox comboPrecioCompra;
    private javax.swing.JLabel estimado;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel labelPrecioCompra;
    private javax.swing.JLabel labelPrecios;
    private javax.swing.JLabel labelPreciosPublico;
    private javax.swing.JLabel nombre;
    private javax.swing.JPanel panelPrecios;
    private javax.swing.JTextField textPrecioCompra;
    // End of variables declaration//GEN-END:variables
}
